package com.example.friend.mvp.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.RecyclerView;

import com.example.friend.R;
import com.example.friend.mvp.model.ModelUser;

import java.util.List;

/**
 * @作者: njb
 * @时间: 2019/8/6 10:27
 * @描述:
 */
public class UserAdapter extends RecyclerView.Adapter<UserAdapter.ViewHolder> {
    private Context mContext;
    private List<ModelUser> mList;
    private int mPosition = -1;


    public UserAdapter(List<ModelUser> fruitList){this.mList=fruitList;}
    public void setAdapterList(List<ModelUser> fruitList){this.mList=fruitList;}

    @Override
    public UserAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if(mContext==null){
            mContext = parent.getContext();
        }
        View view= LayoutInflater.from(mContext).inflate(R.layout.item_user_list,parent,false);
        final ViewHolder holder=new ViewHolder(view);
        return holder;


    }

    @Override
    public void onViewRecycled(ViewHolder holder) {
        super.onViewRecycled(holder);
    }

    @Override
    public void onBindViewHolder(final UserAdapter.ViewHolder holder, int position) {
        ModelUser user = mList.get(position);
        holder.mUserName.setText(user.getUserName());
        holder.mUserDesc.setText(user.getCreateDate());
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    public int getmPosition() {
        return mPosition;
    }

    public class ViewHolder extends RecyclerView.ViewHolder{
        ImageView mImageView;
        TextView mUserName;
        TextView mUserDesc;

        public ViewHolder(View view){
            super(view);
            mImageView= (ImageView) view.findViewById(R.id.iv_user_image);
            mUserName = (TextView) view.findViewById(R.id.tv_user_name);
            mUserDesc = (TextView) view.findViewById(R.id.tv_user_desc);

            view.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {

                    mPosition = getAdapterPosition();
                    return true;
                }
            });
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    v.showContextMenu();
                }
            });
        }

    }

    public ModelUser getItem(int position) {
        if (mList != null && (mList.size() >= position)) {
            return mList.get(position);
        }
        return null;
    }
}
